﻿using Orleans.Runtime;
using Orleans.Streams;
using System;
using System.Threading.Tasks;

namespace Gasgoo.ShareLibrary.Framework.iQueueProvider
{
    public class StreamDeliveryFailureHandler : IStreamFailureHandler
    {
        public StreamDeliveryFailureHandler()
            : this(true)
        {
        }

        public StreamDeliveryFailureHandler(bool faultOnError)
        {
            ShouldFaultSubsriptionOnError = faultOnError;
        }

        public bool ShouldFaultSubsriptionOnError { get; }

        /// <summary>
        /// Should be called when an event could not be delivered to a consumer, after exhausting retry attempts.
        /// </summary>
        public Task OnDeliveryFailure(GuidId subscriptionId, string streamProviderName, IStreamIdentity streamIdentity,
            StreamSequenceToken sequenceToken)
        {
            // TODO 连续的消费失败会放到这里
            Console.WriteLine("\n!!!! OnDeliveryFailure {0} {1} {2} {3}\n", subscriptionId, streamProviderName, streamIdentity, sequenceToken);
            return Task.CompletedTask;
        }

        public Task OnSubscriptionFailure(GuidId subscriptionId, string streamProviderName, IStreamIdentity streamIdentity,
            StreamSequenceToken sequenceToken)
        {
            Console.WriteLine("\n!!!! OnSubscriptionFailure {0} {1} {2} {3} \n", subscriptionId, streamProviderName, streamIdentity, sequenceToken);
            return Task.CompletedTask;
        }
    }
}
